/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.OutletManagement;

import session.TX.AreaSBRemote;
import session.TX.OutletSBRemote;
import session.TX.TimeslotBeanRemote;
import entity.tx.OutletSeatArea;
import entity.tx.Timeslot;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean(name = "timeslotMgmtBean")
@ViewScoped
public class TimeslotMB {

    @EJB
    OutletSBRemote outletBean;
    @EJB
    TimeslotBeanRemote timeslotBean;
    @EJB
    private AreaSBRemote areaBean;
    private List<Timeslot> timeslots;
    private List<OutletSeatArea> areas;
    private Long areaId = new Long(0);
    private String hour;
    private String minute;
    private String statusMsg;
    private Integer selectedIntegerSlot;
    private Integer tabIdx = 0;

    /** Creates a new instance of TimeslotMB */
    public TimeslotMB() {
    }

    public List<OutletSeatArea> getAreas() {
        return areas;
    }

    public void setAreas(List<OutletSeatArea> areas) {
        this.areas = areaBean.getAllAreas(retrieveFromSessionOutletID());
        this.areas = areas;
    }

    public Long getAreaId() {
        return areaId;
    }

    public void setAreaId(Long areaId) {
        this.areaId = areaId;
        this.timeslots = null;
    }

    public String getOutletName() {
        return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("outletName").toString();
    }

    public long getOutletId() {
        return retrieveFromSessionOutletID();
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }

    public String getHour() {
        return hour;
    }

    public void setHour(String hour) {
        this.hour = hour;
    }

    public String getMinute() {
        return minute;
    }

    public void setMinute(String minute) {
        this.minute = minute;
    }

    public Integer getSelectedIntegerSlot() {
        return selectedIntegerSlot;
    }

    public void setSelectedIntegerSlot(Integer selectedIntegerSlot) {
        this.selectedIntegerSlot = selectedIntegerSlot;
    }

    public List<String> getAllHours() {
        List<String> hourList = new ArrayList<String>();
        for (int i = 0; i < 24; i++) {
            String item;
            if (i < 10) {
                item = "0" + String.valueOf(i);
            } else {
                item = String.valueOf(i);
            }
            hourList.add(item);
        }
        return hourList;
    }

    public List<String> getAllMinutes() {
        List<String> minuteList = new ArrayList<String>();
        for (int i = 0; i < 60; i++) {
            String item;
            if (i < 10) {
                item = "0" + String.valueOf(i);
            } else {
                item = String.valueOf(i);
            }
            minuteList.add(item);
        }
        return minuteList;
    }

    public List<Timeslot> getTimeslots() {
        if (this.timeslots == null) {
            System.out.println("retrieving from database");
            this.timeslots = getTimeslotsFrmDatabase();
            return this.timeslots;
        } else {
            return this.timeslots;
        }
    }

    public List<Timeslot> getTimeslots(Long areaId) {
        if (this.areaId.longValue() == areaId.longValue()) {
            if (this.timeslots == null) {
                System.out.println("retrieving from database");
                this.timeslots = getTimeslotsFrmDatabase();
            }
            return this.timeslots;
        } else {
            this.areaId = areaId;
            this.timeslots = getTimeslotsFrmDatabase();
            return this.timeslots;
        }
    }

    public List<Timeslot> getTimeslotsFrmDatabase() {
        return timeslotBean.getTimeSlots(retrieveFromSessionOutletID(), areaId);
    }

//    public void populate(ActionEvent event) {
//        outletBean.populateTimeSlot(trackerManagedOutletMB.getOutletName(), day);
//    }
    public void removeTimeslot(ActionEvent event) {
        timeslots = null;
        Integer time = (Integer) event.getComponent().getAttributes().get("time");
        System.out.println(time);
        if (time != null) {
            statusMsg = timeslotBean.removeTimeSlot(retrieveFromSessionOutletID(), areaId, time);
        } else {
            statusMsg = "nothing is deleted";
        }
    }

    public void addTimeslot(ActionEvent event) {
        timeslots = null;
        Integer time = Integer.parseInt(hour + minute);
        statusMsg = timeslotBean.addTimeSlot(retrieveFromSessionOutletID(), areaId, time);
    }

    public void onEditRow(RowEditEvent event) {
        Timeslot timeslot = (Timeslot) event.getObject(); // Get object from event
        System.out.println("timeslot mgmt bean: about to print selected timeslot");
        System.out.println(timeslot.toString()); // This prints data
        // not the data I wrote into the form
        timeslotBean.saveTimeslot(timeslot);    // Write into the database
    }

    //Randall: Added this method to pull out the outlet ID from session
    public Long retrieveFromSessionOutletID() {
        return (Long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("outletId");
    }

    public Integer getTabIdx() {
        return tabIdx;
    }

    public void setTabIdx(Integer tabIdx) {
        this.tabIdx = tabIdx;
    }
}
